﻿using Newtonsoft.Json;
using System.IO;

namespace KstopaCommon
{
    public class JsonSerializeHelp
    {
        /// <summary>
        /// 将对象序列化后保存到json文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="obj"></param>
        public static void ObjectToJsonfile(string path, object obj)
        {
            string str = JsonConvert.SerializeObject(obj, Formatting.Indented);
            File.WriteAllText(path, str);
        }

        /// <summary>
        /// 将json文件转为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T JsonfileToObject<T>(string path)
        {
            string str = File.ReadAllText(path);
            return JsonConvert.DeserializeObject<T>(str);
        }

    }
}
